/**
 * @author 徐楠
 * @date 2022/1/5 16:18
 * @version 1.0
 */

package com.xunan.likou;

import java.util.*;

public class TheThirdLargestNumber {
    public static void main(String[] args) {
        int[] nums = {1,1,2};
        int result = thirdMax(nums);
        System.out.println("-----");
        System.out.println(result);
    }

    public static int thirdMax(int[] nums) {
        int length = nums.length;
        if (length == 0){
            return 0;
        }else if (length == 1){
            return nums[0];
        }else if (length == 2) {
            return Math.max(nums[0],nums[1]);
        }
        /*String[] numString = new String[];
        for (int i = 0; i < nums.length; i++) {
            numString[i] = nums[i] + "";
        }*/
        //ArrayList<String> temp = new ArrayList<String>(Arrays.asList(numString));
        ArrayList<Integer> temp = new ArrayList<Integer>();
        for (int i = 0; i < nums.length; i++) {
            temp.add(nums[i]);
        }

        HashSet<Integer> set = new HashSet<Integer>(temp);
        ArrayList<Integer> resultTemp = new ArrayList<Integer>(set);
        Collections.sort(resultTemp, new Comparator<Integer>() {

            @Override
            public int compare(Integer o1, Integer o2) {
                // 升序
                //return o1.getAge()-o2.getAge();
                return o2.compareTo(o1);
                // 降序
                // return o2.getAge()-o1.getAge();
                // return o2.getAge().compareTo(o1.getAge());
            }
        });
        for (Integer integer : resultTemp) {
            System.out.println(integer);
        }

        //Integer[] array = (Integer[])resultTemp.toArray(new Integer[nums.length]);

        int result;
        if (resultTemp.size() >= 3){
            if (resultTemp.get(2) != null) {
                result = resultTemp.get(2);
                return result;
            } else  {
                result = resultTemp.get(0);
                return result;
            }
        }else {
            return resultTemp.get(0);
        }


    }
}
